home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Demo / example0 / checktext.py
Encoding:
Python Source  |  2000-06-23  |  1.0 KB  |  40 lines

  1. """checktext - Check that a text file has macintosh-style newlines"""
  2.  
  3. import macfs
  4. import sys
  5. import EasyDialogs
  6. import string
  7.  
  8. def main():
  9.     fsspec, ok = macfs.PromptGetFile('File to check end-of-lines in:', 'TEXT')
  10.     if not ok:
  11.         sys.exit(0)
  12.     pathname = fsspec.as_pathname()
  13.     fp = open(pathname, 'rb')
  14.     try:
  15.         data = fp.read()
  16.     except MemoryError:
  17.         EasyDialogs.Message('Sorry, file is too big.')
  18.         sys.exit(0)
  19.     if len(data) == 0:
  20.         EasyDialogs.Message('File is empty.')
  21.         sys.exit(0)
  22.     number_cr = string.count(data, '\r')
  23.     number_lf = string.count(data, '\n')
  24.     if number_cr == number_lf == 0:
  25.         EasyDialogs.Message('File contains no lines.')
  26.     if number_cr == 0:
  27.         EasyDialogs.Message('File has unix-style line endings')
  28.     elif number_lf == 0:
  29.         EasyDialogs.Message('File has mac-style line endings')
  30.     elif number_cr == number_lf:
  31.         EasyDialogs.Message('File probably has MSDOS-style line endings')
  32.     else:
  33.         EasyDialogs.Message('File has no recognizable line endings (binary file?)')
  34.     sys.exit(0)
  35.     
  36. if __name__ == '__main__':
  37.     main()
  38.     
  39.     
  40.